package com.bwm.bwm007.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bwm.bwm007.R;
import com.bwm.bwm007.main.adapter.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class GuidPagerActivty extends Activity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    //Viewpager
    private ViewPager mViewPager;
    //适配器
    private ViewPagerAdapter mAdapter;
    //图片List
    private List<View> mLists;
    //下标点集
    private ImageView[] mDianView;
    //页面图片合集
    private static final int[] mPics = {R.mipmap.guide1, R.mipmap.guide2, R.mipmap.guide3, R.mipmap.guide4};
    //记录当前选中位置
    private int currentIndex;
    //点
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guid_pager_activty);
        //初始化界面
        InitViews();
    }

    private void InitViews() {
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GuidPagerActivty.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        //初始化引导页图片
        mLists = new ArrayList<>();
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        for (int i = 0; i < mPics.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(mParams);
            iv.setImageResource(mPics[i]);
            mLists.add(iv);
        }
        //初始化viewpager
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mAdapter = new ViewPagerAdapter(mLists);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOnPageChangeListener(this);
        //初始化底部
        initdianviews();

    }

    private void initdianviews() {

        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        mDianView = new ImageView[mPics.length];
        //循环给小点赋予特性
        for (int i = 0; i < mPics.length; i++) {
            mDianView[i] = (ImageView) ll.getChildAt(i);
            mDianView[i].setEnabled(true);
            mDianView[i].setOnClickListener(this);
            mDianView[i].setTag(i);
        }
        currentIndex = 0;
        mDianView[currentIndex].setEnabled(false);//设置为白色，即�?中状�?
    }

    //定位当前引导页
    private void dingweiPage(int position) {
        if (position < 0 || position >= mPics.length) {
            return;
        }
        mViewPager.setCurrentItem(position);
    }

    //定位当前小点
    private void dingweiDian(int position) {
        if (position < 0 || position > mPics.length - 1 || position == currentIndex) {
            return;
        }
        mDianView[position].setEnabled(false);
        mDianView[currentIndex].setEnabled(true);
        currentIndex = position;
    }

    @Override
    public void onClick(View v) {
        int index = (int) v.getTag();
        dingweiDian(index);
        dingweiPage(index);
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {

        dingweiDian(i);
        if (i == 3) {
            mButton.setVisibility(View.VISIBLE);
        } else {
            mButton.setVisibility(View.GONE);
        }

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
